package com.wy;

import org.springframework.web.client.RestTemplate;
import org.json.JSONObject;

// 免费的但是获取不成功。
public class IpApi {
    public static void main(String[] args) {
        String geoLocation = getGeoLocation("114.246.236.19");
        System.out.println(geoLocation);
    }


    public static String getGeoLocation(String ip) {
        String url = "https://ip-api.com/json/" + ip;
        RestTemplate restTemplate = new RestTemplate();
        String response = restTemplate.getForObject(url, String.class);
        JSONObject json = new JSONObject(response);
        if ("success".equals(json.getString("status"))) {
            String country = json.getString("country");
            String region = json.getString("regionName");
            String city = json.getString("city");
            String lat = json.getString("lat");
            String lon = json.getString("lon");
            return "Country: " + country + ", Region: " + region + ", City: " + city + ", 纬度: " + lat + ", 精度: " + lon;
        } else {
            return "Unable to fetch location.";
        }
    }

}

